home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 3: Developer Tools / Linux Cubed Series 3 - Developer Tools.iso / utils / shell / xd-2.08 / xd-2 / xd / Command / getpatte.cc < prev    next >
Encoding:
C/C++ Source or Header  |  1994-08-14  |  3.4 KB  |  145 lines

  1. #include "Command.h"
  2. #include <stdio.h>
  3.  
  4. char const *Command::get_pattern()
  5. {                    // the command contains
  6.                     // /-terminated arguments
  7.                     
  8.                     // create room for the pattern
  9.     pattern = new char [3 * strlen(command) +
  10.                 strlen(config->get_home()) + 2];
  11.  
  12. /*
  13.  
  14.     Possibilities:
  15.         either with or without using 'home no'. When a special case
  16.         arises, it is indicated.
  17.  
  18.     ----------------------------------------------------------
  19.                         sub-specification
  20.     ----------------------------------------------------------
  21.     intention        no            yes
  22.     ----------------------------------------------------------
  23.     from current        .abc (11)        ./a/bc (12)
  24.                             ./abc
  25.     from $HOME               abc  (21)        -a/bc  (22)
  26.     (using the default:                   -abc
  27.         'home yes')
  28.          
  29.     from $HOME (home no)    no special facilities. This reduces
  30.                 to entries 21/22, evaluated from
  31.                 the root.
  32.          
  33.     from /            /abc (31)        /a/bc  (32)
  34.                             //abc
  35.     from parent #        #abc (41)        #a/bc  (42)
  36.     (#: [0-9]                    #/abc
  37.     ----------------------------------------------------------
  38.  
  39.     command[0] determines the initial cell:
  40.         .    indicates from the current directory onward
  41.         /    indicates from the root
  42.         -    indicates subspecifications from $HOME
  43.         #    (#: [0-9]) indicates specifications from parent #
  44.         other    indicates from $HOME
  45.  
  46.         any / or - beyond command[0] automatically switches to
  47.         sub-specifications (the last / on command is not counted
  48.         here, as this one was added by Command() itself.
  49.         
  50. */        
  51.  
  52.     command [strlen(command) - 1] = 0;    // remove the / at the end.
  53.  
  54.     register char
  55.         *next = command;        // next to look at
  56.     register int
  57.         sub_pattern = 0;        // sub_pattern flag
  58.         
  59.     switch (*next)                // do what's appropriate
  60.     {
  61.         case 0:                // degenerate case where
  62. #ifdef DEBUG
  63.     fprintf(stderr, "Command::get_pattern():\t *next == 0\n");
  64. #endif
  65.             strcpy(pattern, "/");    // xd / is specified
  66.         break;
  67.         
  68.         case '0':            // degenerate cells 41, 42
  69.         case '.':            // cells 11, 12
  70.             strcpy(pattern, "./");    // start at current
  71.             next++;            // . processed
  72.         break;
  73.  
  74.         case '-':            // cell 22: from $HOME
  75.             strcpy(pattern, config->get_home());
  76.             sub_pattern++;        // forced subpatterns
  77.             next++;
  78.         break;
  79.  
  80.         case '/':            // cells 31, 32
  81.             strcpy(pattern, "/");    // start from the root
  82. /*            
  83.             sub_pattern =        // sub-patterns for home: no
  84.                 config->get_homeparam()
  85.                 ==
  86.                 Config::from_the_root;
  87. */                
  88.             next++;
  89.         break;
  90.  
  91.         case '1':            // cells 41, 42
  92.         case '2':
  93.         case '3':
  94.         case '4':
  95.         case '5':
  96.         case '6':
  97.         case '7':
  98.         case '8':
  99.         case '9':
  100.             *pattern = 0;        // empty pattern
  101.  
  102.                         // append all parents
  103.                         for (register int count = *next - '0'; count; count--)
  104.                 strcat(pattern, "../");
  105.             next++;
  106.         break;
  107.  
  108.         default:            // cell 21: all other chars
  109.             strcpy(pattern, config->get_home());
  110.     }
  111.  
  112. #ifdef DEBUG
  113.     fprintf(stderr, "Command::get_pattern():\t Command: %s\n"
  114.             "Command::get_pattern():\t Next:    %s\n",
  115.                         command, next);
  116. #endif
  117.             
  118.     if
  119.     (
  120.         *next                // there must be something
  121.         &&                // before it's considered
  122.         (                // a subpattern
  123.             sub_pattern        // sub_patt. already known ?
  124.             ||
  125.             strpbrk(next, "-/")    // or actually found ?
  126.         )
  127.     )
  128.     {
  129. #ifdef DEBUG
  130.         fprintf(stderr, "Command::get_pattern():\t "
  131.                     "separated pattern\n");
  132. #endif
  133.         separated_pattern(next);    // set up separated pattern
  134.     }
  135.     else
  136.     {
  137. #ifdef DEBUG
  138.         fprintf(stderr, "Command::get_pattern():\t single pattern\n");
  139. #endif
  140.         one_pattern(next);        // set up single pattern
  141.     }
  142.  
  143.     return (pattern);
  144. }
  145.